<template>
  <div class="dashboard">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card>
          <div class="data-item">
            <div class="data-title">总文章数</div>
            <div class="data-value">{{ statistics.totalArticles }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div class="data-item">
            <div class="data-title">爬虫文章</div>
            <div class="data-value">{{ statistics.crawlerArticles }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div class="data-item">
            <div class="data-title">图片资源</div>
            <div class="data-value">{{ statistics.totalImages }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div class="data-item">
            <div class="data-title">爬虫任务</div>
            <div class="data-value">{{ statistics.crawlerTasks }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-4">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近文章</span>
              <el-button link @click="viewMore">查看更多</el-button>
            </div>
          </template>
          <el-table :data="recentArticles" stripe>
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="createTime" label="创建时间" width="180" />
            <el-table-column prop="type" label="类型" width="100">
              <template #default="{ row }">
                <el-tag :type="row.type === 'admin' ? '' : 'warning'">
                  {{ row.type === 'admin' ? '管理员' : '爬虫' }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>爬虫任务状态</span>
            </div>
          </template>
          <el-table :data="crawlerTasks" stripe>
            <el-table-column prop="name" label="任务名称" />
            <el-table-column prop="pageCount" label="进度" />
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getTaskStatusType(row.status)">
                  {{ getTaskStatusText(row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="updateTime" label="最后运行" width="180" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getStatistics } from '@/api/dashboard'
import {ElMessage} from "element-plus";

const router = useRouter()

const statistics = ref({
  totalArticles: 0,
  crawlerArticles: 0,
  totalImages: 0,
  crawlerTasks: 0
})

const recentArticles = ref([])
const crawlerTasks = ref([])

// 获取任务状态对应的 tag 类型
const getTaskStatusType = (status) => {
  const statusTypeMap = {
    INIT: 'info',       // 灰色
    RUNNING: 'success', // 绿色
    STOPPED: 'warning', // 黄色
    ERROR: 'danger'     // 红色
  }
  return statusTypeMap[status]
}

// 获取任务状态的中文说明
const getTaskStatusText = (status) => {
  const statusMap = {
    INIT: '初始化',
    RUNNING: '运行中',
    STOPPED: '已停止',
    ERROR: '错误'
  }
  return statusMap[status] || status
}

// 加载控制台数据
const loadDashboardData = async () => {
  try {
    const { data } = await getStatistics()
    statistics.value = data.statistics
    recentArticles.value = data.recentArticles
    crawlerTasks.value = data.crawlerTasks
  } catch (error) {
    ElMessage.error('获取统计数据失败')
  }
}

// 查看更多文章
const viewMore = () => {
  router.push('/admin-articles/list')
}

onMounted(() => {
  loadDashboardData()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.data-item {
  text-align: center;
  padding: 20px 0;
}

.data-title {
  font-size: 14px;
  color: #999;
  margin-bottom: 10px;
}

.data-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.mt-4 {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
